package tr.richfacesext.components.chart;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import tr.richfacesext.components.ComponentConstants;

import com.googlecode.charts4j.AbstractGraphChart;
import com.googlecode.charts4j.AxisLabels;
import com.googlecode.charts4j.AxisLabelsFactory;
import com.googlecode.charts4j.AxisStyle;
import com.googlecode.charts4j.AxisTextAlignment;
import com.googlecode.charts4j.BarChart;
import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.DataUtil;
import com.googlecode.charts4j.Fills;
import com.googlecode.charts4j.GChart;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.LinearGradientFill;
import com.googlecode.charts4j.PieChart;
import com.googlecode.charts4j.Plot;
import com.googlecode.charts4j.Plots;
import com.googlecode.charts4j.Slice;

/**
 * 
 * @author mert
 *
 */
public class ChartHelper {

	public static PieChart createPieChart(List<Number> chartData, List<String> chartColors, List<String> chartLegends) {
		List<Slice> slices = new ArrayList<Slice>();
		int metaDataIndex = 0;
		
		List<Number> percentages = getPercentages(chartData);
		for (Number number : percentages) {
			slices.add(Slice.newSlice(number.intValue(), Color.newColor(chartColors.get(metaDataIndex)), chartLegends.get(metaDataIndex)));
			metaDataIndex++;
		}
		
		PieChart chart = GCharts.newPieChart(slices);
		chart.setThreeD(true);
		setFill(chart);
		chart.setURLEndpoint(getUrlEndpoint());

		return chart;
	}
	
	private static String getUrlEndpoint() {
		return getServerURL() + getServletContext() + ComponentConstants.FACES_PREFIX + ComponentConstants.RICHFACESEXT_CHART_VIEW_ID;
	}

	private static List<Number> getPercentages(List<Number> chartDataSeries) {
		List<Number> result = new ArrayList<Number>();
		
		double total = 0.0;
		for (Number number : chartDataSeries) {
			total += number.doubleValue();
		}
		for (Number number : chartDataSeries) {
			result.add(Math.round(number.doubleValue() / total * 100.0));
		}
		return result;
	}
	
	private static void setFill(AbstractGraphChart chart) {
		chart.setBackgroundFill(Fills.newSolidFill(Color.WHITE));
		LinearGradientFill fill = Fills.newLinearGradientFill(0, Color.WHITE, 100);
		fill.addColorAndOffset(Color.WHITE, 0);
		chart.setAreaFill(fill);
	}	
	
	public static String getChartURL(GChart chart) {
		if (chart == null)
			return StringUtils.EMPTY;
		return chart.toURLString();
	}
	
	private static String getServerURL() {
		FacesContext context = FacesContext.getCurrentInstance();
		//TODO portlets?
		HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();

		String scheme = req.getScheme(); // http
		String serverName = req.getServerName(); // hostname.com
		int serverPort = req.getServerPort(); // 80

		return scheme + "://" + serverName + ":" + serverPort;
	}	
	
	private static String getServletContext() {
		FacesContext context = FacesContext.getCurrentInstance();
		//TODO portlets?
		HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
		
		return req.getContextPath();
	}	
}